﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public Bird bird;
    public GameObject[] StonePrefabs;
    public GameObject[] StoneRPrefabs;

    public GameObject GoldPrefab;
    public GameObject BombPrefab;
    public int Score;

    public Image Black;
    public Image Death;
    public Image Logo;
    public Text ScoreText;
    public Text BestText;
    public Button RestartBtn;
    public Button StartBtn;

    public AudioClip GoldSound;
    public AudioClip HitSound;
    public AudioClip BoomSound;

    private AudioSource audioSource;

    private bool failed;
    private bool start;

    private List<Stone> StoneList;
    private List<Gold> GoldList;
    private List<Bomb> BombList;
    private int Best;

    private Vector3 vector;
    private Vector3 StartPos;

    //障碍物y轴随机范围
    private readonly int minY = 3;
    private readonly int maxY = 12;

    /// <summary>
    /// 障碍物的半高
    /// </summary>
    private readonly int halfHeight = 6;

    private float difficulty;
    // Start is called before the first frame update
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        RestartBtn.onClick.AddListener(() => OnRestartBtnDown());
        StartBtn.onClick.AddListener(() => OnStartBtnDown());
        StartPos = new Vector3(11.24f, 0, 0);
        StoneList = new List<Stone>();
        GoldList = new List<Gold>();
        BombList = new List<Bomb>();
        RestartBtn.gameObject.SetActive(false);
        Death.enabled = false;
        failed = false;
        start = false;
        Score = 0;
        Best = PlayerPrefs.GetInt("Best");
        BestText.text = Best.ToString();
    }

    // Update is called once per frame
    void Update()
    {
        if (failed || !start)
        {
            return;
        }
        foreach (Gold item in GoldList)
        {
            if (item)
            {
                item.Move();
                if (item.gameObject.transform.position.x <= -12)
                {
                    Destroy(item.gameObject);
                }
            }
        }
        foreach (Stone item in StoneList)
        {
            if (item)
            {
                item.Move();
                if (item.gameObject.transform.position.x <= -12)
                {
                    Destroy(item.gameObject);
                }
            }
        }
        foreach (Bomb item in BombList)
        {
            if (item)
            {
                item.Move();
                if (item.gameObject.transform.position.x <= -12)
                {
                    Destroy(item.gameObject);
                }
            }
        }

        bird.Grav();
        if (Input.GetMouseButtonDown(0))
        {
            bird.Fly();
        }
        ScoreText.text = Score.ToString();
    }
    /// <summary>
    /// 初始化
    /// </summary>
    private void GameInitialize()
    {
        foreach (Stone item in StoneList)
        {
            if (item != null)
            {
                Destroy(item.gameObject);
            }
        }
        foreach (Gold item in GoldList)
        {
            if (item != null)
            {
                Destroy(item.gameObject);
            }
        }
        foreach (Bomb item in BombList)
        {
            if (item != null)
            {
                Destroy(item.gameObject);
            }
        }
        Black.enabled = false;
        Death.enabled = false;
        RestartBtn.gameObject.SetActive(false);
        failed = false;
        Score = 0;
        difficulty = 0;
        Best = PlayerPrefs.GetInt("Best");
        BestText.text = Best.ToString();
        InvokeRepeating("GenerateStone", 0, 3);
        InvokeRepeating("GenerateGold", 1.5f, 3);
        InvokeRepeating("GenerateBomb", 3f, 6);
    }
    /// <summary>
    /// 游戏结束
    /// </summary>
    public void GameOver()
    {
        bird.Stop();
        CancelInvoke();
        failed = true;
        Black.enabled = true;
        Death.enabled = true;
        RestartBtn.gameObject.SetActive(true);
        if (Score > Best)
        {
            PlayerPrefs.SetInt("Best", Score);
        }
    }

    private void GenerateStone()
    {
        int idx = Random.Range(0, 2);
        int y = Random.Range(minY, maxY);
        vector = new Vector3(13f, y);
        float gap = 2 * halfHeight + Random.Range(4 - difficulty, 6 - difficulty);
        GameObject go = GameObject.Instantiate(StonePrefabs[idx], vector, Quaternion.identity);
        GameObject goR = GameObject.Instantiate(StoneRPrefabs[idx]);
        goR.transform.position = vector + Vector3.down * gap;

        Stone stone = go.GetComponent<Stone>();
        Stone stoneR = goR.GetComponent<Stone>();
        StoneList.Add(stone);
        StoneList.Add(stoneR);
        if (difficulty <= 2)
        {
            difficulty += 0.1f;
        }
        //同时生成金币
        Vector3 goldpos = new Vector3(13, Random.Range(y - gap + halfHeight + 0.8f, y - halfHeight - 0.8f));
        if (goldpos.y <= -4)
        {
            return;
        }
        GameObject g = Instantiate(GoldPrefab);
        Gold gold = g.GetComponent<Gold>();
        GoldList.Add(gold);
        g.transform.position = goldpos;
    }

    private void OnRestartBtnDown()
    {
        bird.ResetPos();
        GameInitialize();
    }

    private void OnStartBtnDown()
    {
        StartBtn.gameObject.SetActive(false);
        Logo.enabled = false;
        start = true;
        GameInitialize();
    }

    public void PlayGoldSound()
    {
        audioSource.PlayOneShot(GoldSound);
    }

    public void PlayHitSound()
    {
        audioSource.PlayOneShot(HitSound);
    }

    public void PlayBoomSound()
    {
        audioSource.PlayOneShot(BoomSound);
    }

    private void GenerateGold()
    {
        int y = Random.Range(-4, 4);
        Vector3 pos = new Vector3(13, y);
        GameObject g = Instantiate(GoldPrefab, pos, Quaternion.identity);
        Gold gold = g.GetComponent<Gold>();
        GoldList.Add(gold);
    }

    private void GenerateBomb()
    {
        int y = Random.Range(-4, 4);
        Vector3 pos = new Vector3(13, y);
        GameObject b = Instantiate(BombPrefab, pos, Quaternion.identity);
        Bomb bomb = b.GetComponent<Bomb>();
        BombList.Add(bomb);
    }
}
